<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\usermodule\controllers;

use app\models\CouponRecorder;
use app\models\CouponuserRecorder;
use app\models\OrderRecorder;
use app\models\PaylogRecorder;

class UserController extends BaseController{

    public function actionPaynum(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $list = PaylogRecorder::find();
        $list = $list->where('user_id='.$this->user->id)->orderBy('addtime DESC')->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            switch($v['pay_type']){
                case 1:
                    $list[$k]['pay_type_name']="[余额支付]";
                    break;
                case 2:
                    $list[$k]['pay_type_name']="[微信支付]";
                    break;
                case 3:
                    $list[$k]['pay_type_name']="[积分消费]";
                    break;
            }
            //获取订单
            $order = OrderRecorder::find()->where('id='.$v['order_id'])->one();
            $list[$k]['order_name'] = empty($order)?"":$order->order_name;
        }
        if(count($list)>0){
            $result = ['result'=>1,'data'=>$list];
        }else{
            $result = ['result'=>2,'data'=>"没有更多内容"];
        }
        return json_encode($result);



    }


    public function actionYhq(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $list = CouponuserRecorder::find();
        $list = $list->limit($pagesize)->offset($offset)->where('user_id='.$this->user->id)->andWhere('is_del=0')->asArray()->all();
        $arr=[];
        foreach($list as $k=>$v){

            $coupon = CouponRecorder::find()->where('id='.$v['coupon_id'])->one();
            if(empty($coupon)){
                $arr[$k]['reduce'] = 0;
                $arr[$k]['name'] = "优惠券不存在，请删除";
                $arr[$k]['count'] = 0;
            }else{
                $arr[$k]['reduce'] = $coupon->reduce;
                $arr[$k]['count'] = $coupon->count;
                $arr[$k]['name'] = "消费".$coupon->count."元抵扣".$coupon->reduce."元";
            }
            $arr[$k]['day'] = $coupon->day;
            $time = strtotime("+ ".$coupon->day." days",$v['addtime']);
            $arr[$k]['day_time'] = date('Y-m-d H:i:s',$time);
            $arr[$k]['is_time'] = 0;
            if($time<=time()){
                $arr[$k]['is_time'] = 1;//过期
            }
            $arr[$k]['is_use'] = $v['is_use'];
            $arr[$k]['id'] = $v['id'];

        }
        if($arr){
            $result = ['result'=>1,'data'=>$arr];
        }else{
            $result = ['result'=>0];
        }
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionYhqdel(){
        $id = \Yii::$app->request->post('id');
        $model = CouponuserRecorder::find()->where('id=:id',[':id' => $id])->andWhere('user_id='.$this->user->id)->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->is_del=1;
        $model->save(false);
        $result = ['result'=>1,"data"=>'操作成功'];
        return json_encode($result);
    }


    public function actionInfo(){
        $post = \Yii::$app->request->post('form');
        $this->user->user_pic = empty($post['user_pic'])?\Yii::$app->params['non_pic']:str_replace(\Yii::$app->params['base_url'],"",$post['user_pic']);
        $this->user->user_name = empty($post['user_name'])?"微信用户":$post['user_name'];
        $this->user->user_tel = intval($post['user_tel']);
        $this->user->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }



}
